package jdbc419.Demo;

import jdbc419.entity.dept;
import jdbc419.utils.JDBCUtil;

import java.sql.*;
import java.util.Scanner;

/**
 * @Author:SA
 * @DATA:DATA
 * 1.如何解决sql注入 PreparedStatement  提高sql语句的使用率
 * 2.提高sql语句的使用率  预编译sql
 */


public class JDBCDemo01 {
    public static void main(String[] args) {
        test05();

    }
    public static void test01(){
            Connection connection = null;
            PreparedStatement pstmt = null;
            ResultSet resultSet = null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                String url = "jdbc:mysql://localhost:3306/my?useSSL=false&characterEncoding=utf8&serverTimezone=UTC";
                String user = "root";
                String password = "root";
                connection = DriverManager.getConnection(url, user, password);

                //获取操作数据库的对象   预先传递sql语句 -> sql语句预编译  -> 提高sql语句的使用效率
                String sql = "select *  from dept where dept_name = ? and address = ? ";
                pstmt = connection.prepareStatement(sql);
                //pstmt  在查询之前 需要设置查询的参数
                pstmt.setString(1, "监察部");
                pstmt.setString(2, "上海");
                //执行sql
                resultSet = pstmt.executeQuery();
                dept dept01= null;
                //处理结果集
                while (resultSet.next()){
                    int dept_no = resultSet.getInt("dept_no");
                    String dept_name = resultSet.getString("dept_name");
                    String address = resultSet.getString("address");

                    dept01 = new dept(dept_no,dept_name,address);

                }

                if(dept01!=null){
                    System.out.println("登录成功");
                }else{
                    System.out.println("部门和地址错误");
                }

            } catch (Exception e) {
                e.printStackTrace();
            }finally {



                try {
                    if(resultSet!=null){
                        resultSet.close();
                    }

                } catch ( SQLException throwables) {
                    throwables.printStackTrace();
                }

                try {
                    if(pstmt!=null){
                        pstmt.close();
                    }

                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }


                try {
                    if(connection!=null){
                        connection.close();
                    }
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }

            }
    }

    public static void test02(){
        Connection connection = null;
        PreparedStatement pstmt = null;
        ResultSet resultSet = null;
        try {
            //通过工具类获取连接
            connection = JDBCUtil.getConnection();


            //获取操作数据库的对象   预先传递sql语句 -> sql语句预编译  -> 提高sql语句的使用效率
            String sql = "select *  from dept where dept_name = ? and address = ? ";
            pstmt = connection.prepareStatement(sql);
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入部门名:");
            String  deptname = scanner.nextLine();
            System.out.println("请输入地址:");
            String  ad = scanner.nextLine();

            //pstmt  在查询之前 需要设置查询的参数
            pstmt.setString(1, deptname);
            pstmt.setString(2, ad);
            //执行sql
            resultSet = pstmt.executeQuery();
            dept dept01= null;
            //处理结果集
            while (resultSet.next()){
                int dept_no = resultSet.getInt("dept_no");
                String dept_name = resultSet.getString("dept_name");
                String address = resultSet.getString("address");

                dept01 = new dept(dept_no,dept_name,address);

            }

            if(dept01!=null){
                System.out.println("登录成功");
            }else{
                System.out.println("部门和地址错误");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {//释放资源

            JDBCUtil.closeAll(resultSet,pstmt,connection);
        }
    }

    public static void test03(){
        //插入一条数据到 dept表

        //获取连接
        Connection connection = JDBCUtil.getConnection();
        PreparedStatement pstmt = null;
        String sql = "insert into dept(dept_no,dept_name,address)values(?,?,?)";
        try {
            pstmt = connection.prepareStatement(sql);
            //设置参数
            pstmt.setString(1,"99");
            pstmt.setString(2,"决策部");
            pstmt.setString(3,"武汉");
            //执行sql
            int line = pstmt.executeUpdate();
            System.out.println(line>0?"插入成功":"插入失败");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtil.closeAll(null,pstmt,connection);
        }

    }

    public static void test04(){
        //更新一条数据到 dept表

        //获取连接
        Connection connection = JDBCUtil.getConnection();
        PreparedStatement pstmt = null;
        String sql = "update dept set address =  ? where dept_no=?;";
        try {
            pstmt = connection.prepareStatement(sql);
            //设置参数
            pstmt.setString(1,"广州");
            pstmt.setString(2,"99");
            //执行sql
            int line = pstmt.executeUpdate();
            System.out.println(line>0?"更新成功":"更新失败");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtil.closeAll(null,pstmt,connection);
        }

    }

    public static void test05(){
        //删除一条数据到 dept表

        //获取连接
        Connection connection = JDBCUtil.getConnection();
        PreparedStatement pstmt = null;
        String sql = "delete from dept where dept_no = ?";
        try{
            pstmt = connection.prepareStatement(sql);
            //设置参数
            pstmt.setString(1,"41");
            //执行sql
            int line = pstmt.executeUpdate();
            System.out.println(line>0?"删除成功":"删除失败");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtil.closeAll(null,pstmt,connection);
        }

    }


}
